﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace DMirror
{
    public partial class UI : Form
    {
        public DBWorker d;
        public UI()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtSite.Text == "") return;
            XMLWorker w;
            int i = 1;
            do
            {
                w = new XMLWorker(txtSite.Text, i, 100, ref d);
                i ++;
            } while (w.ToBeContinued);
            
            dv.DataSource = d.allPosts;
            dv.Refresh();
            //if (w.ToBeContinued) MessageBox.Show("Next!");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (txtSite.Text == "") return;
            HTTPPool p = new HTTPPool(
                (int)thrnum.Value ,
                ref d, 
                Path.GetDirectoryName(Application.ExecutablePath) 
                + "\\" + txtSite.Text + "\\");
            button2.Enabled = false;
            button1.Enabled = false;
            txtSite.Enabled = false;
            timer1.Enabled = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (txtSite.Text == "") return;
            dv.DataSource = d.allPosts;
            dv.Refresh();
        }


        private void txtSite_SelectedIndexChanged(object sender, EventArgs e)
        {
            d = new DBWorker(string.Format("{0}\\{1}.sl3",
            Path.GetDirectoryName(
            Application.ExecutablePath),
            txtSite.Text), Path.GetDirectoryName(
            Application.ExecutablePath));
            //dv.DataSource = d.allPosts;
            button3.Text = getStatus();
            button5.Enabled = true;
            button6.Enabled = true;

        }

        

        private void UI_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            button3.Text = getStatus();
        }
        private string getStatus()
        {
            d.getStatus();
            return string.Format("{0}  Total:{1} -- Done:{2}",
                "Downloading",
                d.TotalPic.ToString(),
                d.DonePic.ToString());
        }

        private void button4_Click(object sender, EventArgs e)
        {
            d.getShopList((int)thrnum.Value);
            MessageBox.Show(txtSite.Text+".lst was created");
            button3.Text = getStatus();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string listfn = Path.GetDirectoryName(Application.ExecutablePath) + "\\" + txtSite.Text + "_init.lst";
            if(!File.Exists(listfn)){
            FileStream fs = new FileStream(listfn, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            string siteurl = "http://{0}/post/index.xml?limit=1000&page={1}";
            for (int o = 1; o < 1000; o++)
            {
                sw.WriteLine(string.Format(siteurl, txtSite.Text, o.ToString()));
            }
                fs.Flush();
                fs.Close();
            }
            if (!Directory.Exists(Path.GetDirectoryName(Application.ExecutablePath) + "\\" + txtSite.Text + "\\"))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(Application.ExecutablePath) + "\\" + txtSite.Text + "\\");
            }
            DirectoryInfo di = new DirectoryInfo( Path.GetDirectoryName(Application.ExecutablePath) + "\\" + txtSite.Text + "\\");
            FileSystemInfo[] files = di.GetFileSystemInfos("*.xml");
            XMLWorker w;
            for(int i=0; i< files.Length;i++)
            {
                w = new XMLWorker(files[i].FullName,ref d);
                
               // File.Delete(files[i].FullName);
            }
            w = null; files = null; di = null;
            

        }

        private void button6_Click(object sender, EventArgs e)
        {
            d.SyncFile();
            button3.Text = getStatus();
        }

    }
}
